package com.lca.day3zhoukao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lca.day3zhoukao.entity.Goods;
import com.lca.day3zhoukao.entity.Product;
import com.lca.day3zhoukao.service.GoodsService;
import com.lca.day3zhoukao.service.ProductService;
import com.lca.day3zhoukao.vo.SearchVo;
import org.springboot.bw.struct.PageResult;
import org.springboot.bw.struct.Result;
import org.springboot.bw.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：lca
 * @since 2022-07-06
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {


    @Resource
    GoodsService goodsService;

    @Resource
    ProductService productService;

    @PostMapping("search/{currentpage}/{pagesize}")
    public PageResult search(@PathVariable("currentpage") Integer currentpage,
                             @PathVariable("pagesize") Integer pagesize,
                             @RequestBody SearchVo vo){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(vo)){
            if (StringUtils.isNotEmpty(vo.getName())){
                queryWrapper.like("name",vo.getName());
            }
            if (StringUtils.isNotEmpty(vo.getProductname())){
                queryWrapper.eq("productname",vo.getProductname());
            }
        }
        Page<Goods> tpage = new Page<>(currentpage, pagesize);
        Page<Goods> page = this.goodsService.page(tpage, queryWrapper);
        PageResult<Goods> of = PageResult.of(page.getRecords(), page.getTotal());
        return of;
    }

//    @PostMapping("add")
//    public Result add(@RequestBody() Goods goods){
//        Product product = new Product();
//        product.setId(goods.getId());
//        product.setProductname(goods.getProductname());
//        boolean save = this.productService.save(product);
//
//    }
}
